package com.demo.ctrl;

import com.demo.pojo.Author;
import com.demo.pojo.Book;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author zhangxiangnan
 * @date 2021/8/18  21:54
 */
@RestController
@RequestMapping("/test")
public class TestAdviceController {

    // http://127.0.0.1:9001/demo/test/one?param=zxn
    @GetMapping("/one")
    public String testOne(@RequestParam(name = "param") String param) {
        System.out.println("test方法执行了……");
        if (!StringUtils.isEmpty(param)) {
            throw new RuntimeException("主动抛出异常");
        }
        return "you hava request success";
    }

    // http://127.0.0.1:9001/demo/test/two?param=zxn
    @GetMapping("/two")
    public String testTwo(Model model,
                          @RequestParam(name = "param") String param) {
        Map<String, Object> map = model.asMap();
        System.out.println(map);
        System.out.println(">>>>>>>>>: " + param);
        return "hello controller advice";
    }

    // http://127.0.0.1:9001/demo/test/three?b.name=qz&b.price=12&a.name=ws&a.age=12
    @GetMapping("/three")
    public String testThree(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) {
        System.out.println(book);
        System.out.println(author);
        return "hello controller advice three";
    }

}
